import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_mall_app/pages/details_product/details_product_page.dart';
import '../../model/CategoryModel.dart';
import '../../api/api_methods.dart';
import '../../model/ChildCategoryModel.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class ChildProductList extends StatefulWidget {
  final BxMallSubDto bxMallSubDto;

  const ChildProductList({Key key, this.bxMallSubDto}) : super(key: key);

  @override
  _ChildProductListState createState() => _ChildProductListState();
}

class _ChildProductListState extends State<ChildProductList> {
  var future;

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    var fromData = {
      'categoryId': widget.bxMallSubDto.mallCategoryId,
      'categorySubId': widget.bxMallSubDto.mallSubId,
    };
    return Scaffold(
      body: FutureBuilder(
        future: request("getMallGoods", formData: fromData),
        builder: (BuildContext context, AsyncSnapshot snapshot) {
          if (snapshot.hasData) {
            var data = json.decode(snapshot.data.toString());
            ChildCategoryModel childCategoryModel = ChildCategoryModel.fromJson(data);
            return childCategoryModel.goodsList != null
                ? GridView.count(
                    primary: false,
                    crossAxisCount: 2,
                    mainAxisSpacing: 4.0,
                    crossAxisSpacing: 4.0,
                    childAspectRatio: 3 / 4.5,
                    children: childCategoryModel.goodsList.map((item) {
                      return InkWell(
                        onTap: () {
                          Navigator.of(context).push(MaterialPageRoute(builder: (context) {
                            return DetailsProductPage(id: item?.goodsId);
                          }));
                        },
                        child: Card(
                          elevation: 6,
                          child: Padding(
                            padding: const EdgeInsets.all(8.0),
                            child: Column(
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: <Widget>[
                                CachedNetworkImage(imageUrl: item?.image, fit: BoxFit.cover),
                                SizedBox(height: ScreenUtil().setHeight(16)),
                                Text(
                                  "${item?.goodsName}",
                                  style: TextStyle(
                                      color: Colors.redAccent, fontSize: ScreenUtil().setSp(20)),
                                  textAlign: TextAlign.center,
                                ),
                                SizedBox(height: ScreenUtil().setHeight(16)),
                                Row(
                                  mainAxisAlignment: MainAxisAlignment.center,
                                  children: <Widget>[
                                    Text(
                                      '￥${item?.presentPrice}',
                                      style: TextStyle(fontSize: ScreenUtil().setSp(25)),
                                    ),
                                    SizedBox(width: ScreenUtil().setHeight(16)),
                                    Text(
                                      '￥${item?.oriPrice}',
                                      style: TextStyle(
                                          color: Colors.black26,
                                          fontSize: ScreenUtil().setSp(20),
                                          decoration: TextDecoration.lineThrough),
                                    )
                                  ],
                                ),
                              ],
                            ),
                          ),
                        ),
                      );
                    }).toList(),
                  )
                : Center(child: Text("暂无商品..."));
          } else {
            return Center(
              child: SpinKitFadingCircle(
                itemBuilder: (_, int index) {
                  return DecoratedBox(
                    decoration: BoxDecoration(color: index.isEven ? Colors.red : Colors.green),
                  );
                },
              ),
            );
          }
        },
      ),
    );
  }
}
